package com.teatrace.service.process.impl;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;

import com.teatrace.commons.util.HibernateUtils;
import com.teatrace.model.process.Consist;
import com.teatrace.model.process.DryingStatus;
import com.teatrace.model.process.KilloutStatus;
import com.teatrace.model.process.ProcessFlow;
import com.teatrace.model.process.ProcessType;
import com.teatrace.model.process.RollingStatus;
import com.teatrace.model.produce.MaterialPackage;
import com.teatrace.service.process.ProcessService;

public class ProcessServiceImpl implements ProcessService{

    //更改原料包加工状态为"已杀青"
	//存储杀青状态
	public String AddKilloutStatus(KilloutStatus ks,int MaterialPackageId) {
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		MaterialPackage  m = (MaterialPackage) session.load(MaterialPackage.class,new Integer(MaterialPackageId));
		m.setProcessstatus("已经杀青");
		ks.setMaterialpackage(m);
		session.save(ks);
		session.getTransaction().commit();
		sf.close();
		return "success";
	}
	
	//更改原料包加工状态为"已揉捻"
	//存储揉捻状态
	public String AddRollingStatus(RollingStatus rs) {
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		System.out.println("揉捻状态的ID为："+ rs.getId());
		System.out.println("原料包ID为："+ rs.getMaterialpackage().getId());
		MaterialPackage  m = (MaterialPackage) session.load(MaterialPackage.class,new Integer(rs.getMaterialpackage().getId()));
		m.setProcessstatus("已经揉捻");
		rs.setMaterialpackage(m);
		session.save(rs);
		session.getTransaction().commit();
		sf.close();	
		return "success";
	}
	

	
	//更改原料包加工状态为"已干燥"
	//存储干燥状态
	//加工完成，产生半成品
	public String AddDryingStatus(DryingStatus ds) {
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		MaterialPackage  m = (MaterialPackage) session.load(MaterialPackage.class,new Integer(ds.getMaterialpackage().getId()));
		m.setProcessstatus("已经干燥");
		ds.setMaterialpackage(m);
		session.save(ds);
		session.getTransaction().commit();
		sf.close();
		return "success";
	}
	

	@Test
	public List<KilloutStatus> GetKilloutedPackages() {
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();		
		Query query = session.createQuery("from KilloutStatus r inner join fetch r.materialpackage m " +
				                           "where m.processstatus = '已经杀青'");
		List<KilloutStatus> kss = query.list();
//		for(int i = 0; i<kss.size();i++){
//			KilloutStatus ks = kss.get(i);
//			System.out.println(ks.getId());
//		}
		sf.close();
		return kss;
	}
	
	public List<RollingStatus> GetRolledPackages() {
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();		
		Query query = session.createQuery("from RollingStatus r inner join fetch r.materialpackage m " +
				                           "where m.processstatus = '已经揉捻'");
		List<RollingStatus> rss = query.list();
		sf.close();
		return rss;
	}
	//异常处理try-catch-finally
	//如果从数据库取出的数据为0
	public List<DryingStatus> GetDriedPackages() {
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();		
		Query query = session.createQuery("from DryingStatus  r inner join fetch r.materialpackage m " +
				                            "where m.processstatus = '已经干燥'");
		List<DryingStatus> dss = query.list();
		try{
			System.out.println("干燥记录个数为："+dss.size());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		//System.out.println(dss.get(0).getTime());
		sf.close();
		return dss;
	}
	
	public List<MaterialPackage> GetMaterialPackagesToBeProcessed(){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		List<MaterialPackage> mps = session.createQuery("from MaterialPackage m " +
				                    "where m.processstatus = 'nothing' or m.processstatus = ''").list();
		sf.close();
		return mps;
	}
	
	//根据条件查询实体
	public ProcessType GetProcessType(String name){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		Query query = session.createQuery("from ProcessType p where p.name = '" + name + "'");
		List<ProcessType> p = query.list();
		sf.close();
		if(p.size() == 0) return null;
		return p.get(0);
	}
	//新增加工工序和加工流程的联系
	public String AddConsist(Consist consist){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		session.beginTransaction();
		session.save(consist);
		session.getTransaction().commit();
		sf.close();
		return "success";
	}
	
	//获取所有加工工序
	public List<ProcessType> GetProcessTypes(){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		Query query = session.createQuery("from ProcessType");
		List<ProcessType> pts = query.list();
		sf.close();
		return pts;
	}
	
	//新增加工流程
	public String AddProcessFlow(ProcessFlow pf){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		session.beginTransaction();
		session.save(pf);
		session.getTransaction().commit();
		sf.close();
		return "success";		
	}
	//根据条件查询实体
	public ProcessFlow GetProcessFlow(String name){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		Query query = session.createQuery("from ProcessFlow pf where pf.name = '" + name + "'");
		List<ProcessFlow> pf = query.list();
		sf.close();
		if(pf.size() == 0) return null;		
		return pf.get(0);
	}
	
	public ProcessFlow GetProcessFlow(long processflowid){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		ProcessFlow pf = (ProcessFlow) session.load(ProcessFlow.class, processflowid);
		sf.close();
		return pf;
	}
	
	//获取所有加工流程
	public List<ProcessFlow> GetProcessFlows(){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		Query query = session.createQuery("from ProcessFlow");
		List<ProcessFlow> pfs = query.list();
		sf.close();
		return pfs;		
	}
	
	//删除加工流程
	public String DeleteProcessFlow(long processflowid){
		SessionFactory sf = HibernateUtils.getSessionFactory();
		Session session = sf.openSession();	
		session.beginTransaction();
		ProcessFlow pf = (ProcessFlow) session.load(ProcessFlow.class, processflowid);
		session.delete(pf);
		session.getTransaction().commit();
		sf.close();
		return "success";
	}
}
